package com.shujia;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Code2MysqlConnectForUpdate {
    public static void main(String[] args) throws SQLException {
        // 1.注册驱动
        /**
         * registerDriver(java.sql.Driver driver)
         */
        DriverManager.registerDriver(new Driver());

        // 2.获取连接
        /*
            Connection getConnection(String url,String user, String password)

            URL:格式: jdbc:mysql://IP:PORT/数据库?参数名1=参数值1&参数名2=参数值2
            jdcb: 主协议
            mysql: 次级协议，表示连接MySQL数据库
            IP: Mysql所在服务器地址
            PORT: Mysql端口号(默认3306)

         */

        String url = "jdbc:mysql://192.168.20.90:3306/bigdata19?useSSL=false&useUnicode=true&characterEncoding=utf8";
        String user = "root";
        String password = "123456";
        Connection connection = DriverManager.getConnection(url, user, password);

        Statement statement = connection.createStatement();

        String updateSql = "INSERT INTO dept(DEPTNO,DNAME,LOC) VALUES (50,'教学部','英唐工业园')";
        int i = statement.executeUpdate(updateSql);
        if (i > 0){
            System.out.println("数据更新成功...");
        }else {
            System.out.println("数据更新失败....");
        }

//        statement.executeQuery("INSERT INTO dept(DEPTNO,DNAME,LOC) VALUES (60,'市场部','英唐工业园')");


        statement.close();
        // 关闭连接
        connection.close();
    }
}
